package com.pet_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet_service.entity.OrderHomeService;
import com.pet_service.entity.UserAddress;
import com.pet_service.mapper.OrderHomeServiceMapper;
import com.pet_service.mapper.UserAddressMapper;
import com.pet_service.result.Result;
import com.pet_service.service.OrderHomeServiceService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
* @author 邹全伸
* @description 针对表【order_home_service(上门服务-订单关联表)】的数据库操作Service实现
* @createDate 2025-09-20 09:18:18
*/
@Service
public class OrderHomeServiceServiceImpl extends ServiceImpl<OrderHomeServiceMapper, OrderHomeService>
    implements OrderHomeServiceService{
    @Resource
    private UserAddressMapper userAddressMapper;

    //查询服务订单的状态,PENDING(0, "待执行"),EXECUTING(1, "执行中"),COMPLETED(2, "已完成");
    @Override
    public Result getOrderStatus(Integer id) {
        //获取订单
        OrderHomeService orderHomeService = this.getById(id);
        //判断订单是否存在
        if (orderHomeService != null) {
            return Result.ok(orderHomeService.getService_status());
        }
        return Result.fail();
    }

    @Override
    public boolean physicalDelete(Integer id) {
        if (id == null || id <= 0) {
            return false;
        }

        // 执行物理删除
        int result = baseMapper.deleteByPhysicalId(id);
        return result > 0;
    }

    @Override
    public IPage<OrderHomeService> pageWithAddressInfo(Page<OrderHomeService> page, QueryWrapper<OrderHomeService> queryWrapper) {
        // 先查询订单基础信息
        IPage<OrderHomeService> pageResult = this.page(page, queryWrapper);

        // 处理每个订单，添加地址信息
        List<OrderHomeService> orders = pageResult.getRecords();
        for (OrderHomeService order : orders) {
            // 根据service_address_id查询地址详情
            if (order.getService_address_id() != null) {
                UserAddress address = userAddressMapper.selectById(order.getService_address_id());
                if (address != null) {
                    // 组装完整地址
                    StringBuilder fullAddress = new StringBuilder();
                    if (address.getProvince() != null) fullAddress.append(address.getProvince());
                    if (address.getCity() != null) fullAddress.append(address.getCity());
                    if (address.getDistrict() != null) fullAddress.append(address.getDistrict());
                    if (address.getDetail() != null) fullAddress.append(address.getDetail());
                    order.setFullAddress(fullAddress.toString());
                }
            }
        }

        return pageResult;
    }
}




